<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!-- 搜索 -->
      <el-input v-model="name" clearable placeholder="会员名称" style="width: 200px;" class="filter-item" />
      <el-select v-model="level" clearable placeholder="会员等级" class="filter-item" style="width: 130px">
        <el-option
          v-for="item in statusOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toQuery">搜索</el-button>
      <!-- 新增 -->
      <el-button
        type="danger"
        class="filter-item"
        size="mini"
        icon="el-icon-refresh"
        @click="toQuery"
      >刷新</el-button>
    </div>
    <!--表单组件-->
    <eForm ref="form" :is-add="isAdd" />
    <pForm ref="formp" :is-add="isAdd" />
    <!--表格渲染-->
    <el-table v-loading="loading" :data="data" size="small" style="width: 100%;">
      <el-table-column prop="id" label="会员id" />
      <el-table-column prop="memberName" label="会员名称" />
      <el-table-column ref="table" prop="avatar" label="用户头像">
        <template slot-scope="scope">
          <a :href="scope.row.headimg" style="color: #42b983" target="_blank"><img :src="scope.row.headimg" alt="点击打开" class="el-avatar"></a>
        </template>
      </el-table-column>
      <el-table-column prop="mobile" label="手机号码" />
      <el-table-column prop="nickName" label="会员昵称" />
      <el-table-column label="性别" align="center">
        <template slot-scope="scope">
          <div>
            <el-tag v-if="scope.row.sex == '0'">男</el-tag>
            <el-tag v-else-if="scope.row.sex == '1'">女</el-tag>
            <el-tag v-else>未知</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="integral" label="会员积分" />
      <el-table-column :show-overflow-tooltip="true" prop="birthday" label="会员生日">
        <template slot-scope="scope">
          <span>{{ formatTime(scope.row.birthday) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <div >
            <el-tag v-if="scope.row.status == 0" style="cursor: pointer" :type="''">有效</el-tag>
            <el-tag v-else style="cursor: pointer" :type=" 'info' ">过期</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="等级" align="center">
        <template slot-scope="scope">
          <div>
            <el-tag v-if="scope.row.level == '0'">白银</el-tag>
            <el-tag v-else-if="scope.row.level == '1'">黄金</el-tag>
            <el-tag v-else-if="scope.row.level == '2'">铂金</el-tag>
            <el-tag v-else-if="scope.row.level == '3'">钻石</el-tag>
            <el-tag v-else>王者</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column  label="操作" width="185" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="edit(scope.row)"
          />
         <el-popover
            :ref="scope.row.id"
            placement="top"
            width="180">
            <p>确定删除本条数据吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定</el-button>
            </div>
            <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini"/>
          </el-popover>
         
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination
      :total="total"
      :current-page="page + 1"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"
    />
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import initData from '@/mixins/crud'
import { del, onStatus } from '@/api/User'
import eForm from './form'
import pForm from './formp'
import { formatTime } from '@/utils/index'
export default {
  components: { eForm, pForm },
  mixins: [initData],
  data() {
    return {
      delLoading: false,
      level: '',
      name:'',
      statusOptions: [
        { value: '0', label: '白银' },
        { value: '1', label: '黄金' },
        { value: '2', label: '铂金' },
        { value: '3', label: '钻石' },
        { value: '4', label: '王者' }
      ]
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    formatTime,
    checkPermission,
    onStatus(id, status) {
      this.$confirm(`确定进行[${status ? '禁用' : '开启'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          onStatus(id, { status: status }).then(({ data }) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.init()
              }
            })
          })
        })
        .catch(() => { })
    },
    beforeInit() {
      this.url = 'api/getMember'
      this.params = { page: this.page, size: this.size,level:this.level,memberName:this.name}
      return true
    },
    subDelete(uid) {
      this.delLoading = true
      del(uid).then(res => {
        this.delLoading = false
        this.$refs[uid].doClose()
        this.dleChangePage()
        this.init()
        this.$notify({
          title: '删除成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        this.delLoading = false
        this.$refs[uid].doClose()
        console.log(err.response.data.message)
      })
    },
    add() {
      this.isAdd = true
      this.$refs.form.dialog = true
    },
    edit(data) {
      this.isAdd = false
      const _this = this.$refs.form
      _this.form = {
        id: data.id,
        memberName: data.memberName,
        mobile: data.mobile,
        nickName: data.nickName,
        birthday: data.birthday,
        sex: data.sex,
        address: data.addressmark,
        level: data.level,
        headimg: data.headimg,
        status: data.status,
        integral: data.integral
      }
      _this.dialog = true
    },
    editP(data) {
      this.isAdd = false
      const _this = this.$refs.formp
      _this.form = {
        uid: data.uid,
        nickname: data.nickname,
        ptype: 1,
        money: 0
      }
      _this.dialog = true
    }
  }
}
</script>

<style scoped>

</style>
